package com.benson.common;

import java.text.ChoiceFormat;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Date;
import java.util.Locale;

public class MessageFormatDemo {  
	  
	   static  void displayMessage(Locale currentLocale) {  
	  
	      System.out.println("currentLocale = " + currentLocale.toString());  
	      System.out.println();  
	  
	      Object[] messageArguments = {  
	         "Mars",  
	         new Integer(7),  
	         new Date() 
	      };  
	     
	      MessageFormat formatter = new MessageFormat("");  
	      formatter.setLocale(currentLocale);  
	        
	      String template = "At {2,time,short} on {2,date,long}, we detected " +  
	            "{1,number,integer} spaceships on the planet {0}.";  
	  
	      formatter.applyPattern(template);  
	      String output = formatter.format(messageArguments);  
	  
	      System.out.println(output);  
	      System.out.println(String.format(template, messageArguments));  
	  
	      
	      double[] filelimits = {0,1,2};
	      String[] filepart = {"are no files","is one file","are {2} files"};
	      ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
	      Format[] testFormats = {fileform, null, NumberFormat.getInstance()};
	      MessageFormat pattform = new MessageFormat("There {0} on {1}");
	      pattform.setFormats(testFormats);
	      Object[] testArgs = {null, "ADisk", null};
	      for (int i = 0; i < 4; ++i) {
	          testArgs[0] = new Integer(i);
	          testArgs[2] = testArgs[0];
	          System.out.println(pattform.format(testArgs));
	      }

	   }  
	  
	   static public void main(String[] args) {  
	      displayMessage(new Locale("en", "US")); 
	      
	      double[] limits = {1,2,3,4,5,6,7};
	      String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
	      ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
	      ParsePosition status = new ParsePosition(0);
	      for (double i = 0.0; i <= 8.0; ++i) {
	          status.setIndex(0);
	          System.out.println(i + " -> " + form.format(i) + " -> "
	                                   + form.parse(form.format(i),status));
	      }

	   }  
	}
